package com.zelin.gulimall.product.service.impl;

import com.zelin.gulimall.product.dao.BrandDao;
import com.zelin.gulimall.product.dao.CategoryDao;
import com.zelin.gulimall.product.entity.BrandEntity;
import com.zelin.gulimall.product.entity.CategoryEntity;
import com.zelin.gulimall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zelin.common.utils.PageUtils;
import com.zelin.common.utils.Query;

import com.zelin.gulimall.product.dao.CategoryBrandRelationDao;
import com.zelin.gulimall.product.entity.CategoryBrandRelationEntity;
import com.zelin.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private CategoryBrandRelationService relationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }

    //查询分类关联的品牌
    @Override
    public List<BrandVo> brandList(Long catId) {
        //1.查询分类关联的中间表集合
        List<CategoryBrandRelationEntity> entities = relationService.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<Long> brandIds = entities.stream().map(m -> {
            return m.getBrandId();
        }).collect(Collectors.toList());
        //brandDao.selectList(new QueryWrapper<BrandEntity>().eq(""))
        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
        if(brandIds != null && brandIds.size() > 0){
            queryWrapper.in("brand_id", brandIds);
        }
        List<BrandEntity> brandEntities = brandDao.selectList(queryWrapper);

        //2. 将BrandEntity集合转换为BrandVo集合
        List<BrandVo> collect = brandEntities.stream().map(m -> {
            BrandVo vo = new BrandVo();
            vo.setBrandId(m.getBrandId());
            vo.setBrandName(m.getName());
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

}